<template>
	<view class="add-addaress">
		<form @submit="formSubmit" class="forms">
			<view class="items">
				<view class="names">{{addList[0].name}}</view>
				<input :value="addList[0].val" class="ipt" name="name" placeholder="请输入收件人姓名" />
			</view>
			<view class="items">
				<view class="names">{{addList[1].name}}</view>
				<input :value="addList[1].val" class="ipt" name="tel" placeholder="请输入收件人电话号码" />
			</view>
			<view class="items">
				<view class="names">{{addList[2].name}}</view>
				<view class="ipt" @click="showMulLinkageThreePicker">
					<view v-if="isShow">点击选择地址</view>
					<input v-else class="ipt" name="cityStr" :value="addList[2].val" />
				</view>
				<!-- 下拉地址选择框 -->
				<mpvue-picker :themeColor="themeColor" ref="mpvuePicker" :mode="mode" :deepLength="deepLength" :pickerValueDefault="pickerValueDefault"
				 @onConfirm="onConfirm" @onCancel="onCancel" :pickerValueArray="pickerValueArray"></mpvue-picker>
				<mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValueDefault"
				 @onCancel="onCancel" @onConfirm="onConfirm"></mpvue-city-picker>
				<!-- 下拉地址选择框 -->
			</view>
			<view class="items">
				<view class="names">{{addList[3].name}}</view>
				<input :value="addList[3].val" class="ipt" name="cityBak" placeholder="输入详细地址" />
			</view>
			<view class="items">
				<view class="names">{{addList[4].name}}</view>
				<switch name="isUse" />
				<!-- <input class="ipt" name="cityBak2" placeholder="输入详细地址" /> -->
			</view>
			<view class="btns">
				<view class="main-text">
					<QSWavesButton wavesColor="#fff" class="btn" txt="保存地址" form-type="submit"></QSWavesButton>
				</view>
			</view>
		</form>
	</view>
</template>

<script>
	// https://github.com/zhetengbiji/mpvue-picker
	import mpvuePicker from '@/components/mpvue-picker/mpvuePicker.vue';
	// https://github.com/zhetengbiji/mpvue-citypicker
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue'
	import QSWavesButton from '@/components/QS-WavesButton/QS-WavesButton.vue';
	import {
		getSaveAddress
	} from '@/network/address/index.js'
	export default {
		data() {
			return {
				mode: '',
				themeColor: '#007AFF',
				deepLength: 1,
				pickerText: '',
				pickerValueArray: [],
				cityPickerValueDefault: [0, 0, 1],
				pickerValueDefault: [0], // 默认值
				addList: [{
						name: '收件人',
						val: 'wx',
					},
					{
						name: '电话号码',
						val: '13485565286',
					},
					{
						name: '所在地区',
						val: '',
					},
					{
						name: '详细地址',
						val: '不凡学院',
					},
					{
						name: '设置默认地址',
						val: '',
					}
				],
				cityCode: '',
				isShow: true,
				isUse: false
			};
		},
		components: {
			QSWavesButton,
			mpvuePicker,
			mpvueCityPicker
		},
		methods: {
			async formSubmit(e) {
				e.detail.value.isUse = e.detail.value.isUse ? '1' : '0';
				e.detail.value.cityCode = this.cityCode;
				e.detail.value.tag = '公司';
				e.detail.value.userId = uni.getStorageSync('tokens').id
				console.log(e)
				const datas = e.detail.value
				if (this.addList[2].val) {
					const res = await getSaveAddress(datas)
					console.log(res, '成功')
					if (res.data.code == 'S') {
						uni.navigateTo({
							url: '/pages/address/address'
						})
					}
				} else {
					uni.showToast({
						title: '请填写数据',
						icon: 'none'
					})
				}


			},
			showMulLinkageThreePicker() {
				this.$refs.mpvueCityPicker.show()
			},
			onCancel(e) {
				console.log(e)
			},
			onConfirm(e) {
				this.pickerText = e
				console.log(this.pickerText)
				this.pickerText.cityCode += (',' + this.pickerText.value.join(','))
				this.cityCode = this.pickerText.cityCode
				console.log(this.cityCode)
				this.addList.forEach(item => {
					if (item.name == '所在地区') {
						this.isShow = false // 控制input 框和 点击切换 来回切换
						item.val = this.pickerText.label
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.add-addaress {
		height: 100vh;
		background-color: #F1ECE7;
		padding: 0 30rpx;
		overflow: auto;
	}

	.forms {
		.items {
			display: flex;
			justify-content: space-between;
			padding: 30rpx 0;

			.names {
				width: 30%;
			}

			.ipt {
				width: 70%;
			}

		}
	}

	.btns {
		position: fixed;
		left: 0;
		bottom: 10rpx;
		width: 100%;
		height: 120rpx;
		display: flex;
		justify-content: center;
		align-items: center;

		.main-text {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 70%;

			.btn {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 100%;
				height: 80rpx;
				background-color: #354E44;
				color: #fff;
				font-size: 30rpx;
				border-radius: 40rpx;
				box-shadow: 0px 4rpx 16rpx 0px rgba(0, 0, 0, .4);
				background-color: #354E44;
			}
		}
	}
</style>
